package edu.usc.epigenome.dnaase.script;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.biojava3.genome.parsers.gff.Location;
//import org.biojava3.core.sequence.location.template.Location;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;



public class genomicLocationOverlap {

	/**
	 * @param args
	 */
	final private static String USAGE = "genomicLocationOverlap genomicLocation.txt genomicLocationReference.txt output.txt ...";
	
	@Argument
	private List<String> stringArgs = new ArrayList<String>();
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		new genomicLocationOverlap(args);
	}

	public genomicLocationOverlap(String[] args) throws Exception{
		CmdLineParser parser = new CmdLineParser(this);
		parser.setUsageWidth(80);
		try
		{
			parser.parseArgument(args);
			if (stringArgs.size() < 3) throw new CmdLineException(USAGE);
			
			
		}
		catch (CmdLineException e)
		{
			System.err.println(e.getMessage());
			parser.printUsage(System.err);
			System.err.println();
			return;
		}

		//String chr = stringArgs.remove(0);
		String genomicLocFileName = stringArgs.remove(0);
		String genomicLocRefFileName = stringArgs.remove(0);
		String fn = stringArgs.remove(0);
		BufferedReader genomicLocBr = new BufferedReader(new FileReader(genomicLocFileName));
		BufferedReader genomicLocRefBr = new BufferedReader(new FileReader(genomicLocRefFileName));
		
		PrintWriter outWriter = new PrintWriter(new File(fn));
		HashMap<String,String> genomicLocRefMap= new HashMap<String,String>();
		HashMap<String,List<Location>> genomicLocMap= new HashMap<String,List<Location>>();
		String line;
		int num = 0;
		while( (line = genomicLocRefBr.readLine()) != null ){
			if(num ==0){
				num++;
				continue;
			}
			String[] tmpArray = line.split("\t");
			if(genomicLocMap.containsKey(tmpArray[0])){
				List<Location> tempLoc = genomicLocMap.get(tmpArray[0]);
				Location thisLoc = new Location(Integer.parseInt(tmpArray[1]),Integer.parseInt(tmpArray[2]));
				tempLoc.add(thisLoc);
				genomicLocMap.put(tmpArray[0], tempLoc);
				String keyMark = tmpArray[0] + "~" + tmpArray[1] + "~" + tmpArray[2];
				genomicLocRefMap.put(keyMark, line);
			}
			else{
				List<Location> tempLoc = new ArrayList<Location>();
				Location thisLoc = new Location(Integer.parseInt(tmpArray[1]),Integer.parseInt(tmpArray[2]));
				tempLoc.add(thisLoc);
				genomicLocMap.put(tmpArray[0], tempLoc);
				String keyMark = tmpArray[0] + "~" + tmpArray[1] + "~" + tmpArray[2];
				genomicLocRefMap.put(keyMark, line);
			}
		}
		num=0;
		while( (line = genomicLocBr.readLine()) != null ){
			if(num ==0){
				num++;
				continue;
			}
			String[] tmpArray = line.split("\t");
			Location checkLoc = new Location(Integer.parseInt(tmpArray[1]),Integer.parseInt(tmpArray[2]));
			if(genomicLocMap.containsKey(tmpArray[0])){
				List<Location> thisLoc = genomicLocMap.get(tmpArray[0]);
				Iterator<Location> It = thisLoc.iterator();
				while(It.hasNext()){
					Location tempLoc = It.next();
					if(tempLoc.contains(checkLoc)){
						String keySearch = tmpArray[0] + "~" + tempLoc.getBegin() + "~" + tempLoc.getEnd();
						String content = genomicLocRefMap.get(keySearch);
						outWriter.println(line + "\t" + content);
						System.out.println(tempLoc);
						System.out.println(checkLoc.toString());
						System.out.println(tempLoc.contains(checkLoc));
						break;
					}
					
				}
			}
		}
		outWriter.close();
		
	}
}
